require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
require("../component/cityChoice.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var dataInfo=JSON.parse(sessionStorage.getItem("data"));
var key="myGoodsList-"+userInfo.id;
console.log(key)
var myGoodsList=JSON.parse(sessionStorage.getItem(key));
console.log(myGoodsList)
var categoryName="categoryName-"+userInfo.id;
var categoryN=JSON.parse(sessionStorage.getItem(categoryName));
var cityName="cityName-"+userInfo.id;
var cityN=JSON.parse(sessionStorage.getItem(cityName));
var content="content-"+userInfo.id;
var contentN=JSON.parse(sessionStorage.getItem(content));
var data="data-"+userInfo.id;
var dataList=JSON.parse(sessionStorage.getItem(data));
var vm = new Vue({
    el:"#travelNotes",
    data:{
        datalist:{
            loginUid:userInfo.id,
            categoryId:"",
            // circleName:"",
            coverImgSrc:"",
            coverImgWidth:"",
            coverImgHeight:"",
            title:"",
            cityId:"",
            content:"",
            isShowArea:"0"
        },
        img:"",
        imgNum:"1",
        results:{},
        categoryName:"",
        categoryList:[],
        categoryList1:[],
        message:"选择发布游记城市",
        isScreen:"",
        cityName:"",
        cityName1:"",
        myGoodsList:myGoodsList,
        number:1,
        num:false
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        if(this.searchObject&&this.searchObject.number){
            this.number=this.searchObject&&this.searchObject.number;
            this.categoryName=categoryN;
            this.cityName=cityN;
            this.datalist=dataList;
            this.datalist.content=contentN;
            $('#content').html(contentN);
        }else{
            sessionStorage.removeItem(key);
            sessionStorage.removeItem(categoryName);
            sessionStorage.removeItem(cityN);
            sessionStorage.removeItem(dataList);
            sessionStorage.removeItem(contentN);
        }
        this.getMainAllCategoryList();
        console.log(this.myGoodsList)
        // console.log(this.number)
    },
    methods:{
        //后退
        getBack:function(){
            // sessionStorage.clear(key);
            sessionStorage.removeItem(key);
            sessionStorage.removeItem(categoryName);
            sessionStorage.removeItem(cityN);
            sessionStorage.removeItem(dataList);
            sessionStorage.removeItem(contentN);
           location.href="/see/cityAd"
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //跳转
        getNext:function(cityId){
            location.href="/see/cityAdInfo?cityId="+cityId
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //封面图片
        getChange:function(){
            var that=this;
            that.datalist.coverImgSrc=document.getElementById("fileinput").files[0];//$("#fileinput").files[0];
            that.datalist.coverImgSrc = new File([that.datalist.coverImgSrc], new Date().getTime()+".png",{type:"image/jpeg"});
            lrz(that.datalist.coverImgSrc, function (results) {
                // 你需要的数据都在这里，可以以字符串的形式传送base64给服务端转存为图片。
                console.log(results);
                that.results=results;
                that.imgNum=2;
                demo_report(results.base64);
            });
        },
        //获取所有自媒体分类信息
        getMainAllCategoryList:function(){
            var that=this;
            var list=[];
            dataInfo.allCategorys.forEach(function(val){
                if(val.parentId=="e124ec25353d43c39394d30ff1416879"){
                    list.push(val);
                }
            });
            that.categoryList=list;

        },
        getSelect:function(type){
            var that=this;
            if(type==1){
                var bankSelect = new IosSelect(1,
                    [that.categoryList1],
                    {
                        container: '.provingMode',
                        itemHeight: 30,
                        itemShowCount: 3,
                        callback: function (selectOneObj) {
                            that.datalist.categoryId=selectOneObj.id;
                            that.categoryName=selectOneObj.value;
                            console.log(selectOneObj)
                        }
                    });
            }else if(type==2){
                that.getIsScreen(type);
            }else if(type==3){
                that.number++;
                console.log(that.number)
                //that.getIsScreen(type);
            }
        },
        getIsScreen:function(num){
            this.isScreen=num;
        },
        choiceList:function(data){
            console.log(data);
            this.datalist.cityId=data.cityId;
            this.cityName=data.cityName;
        },
        getisShowArea:function(){
            if(this.datalist.isShowArea==0){
                this.datalist.isShowArea=1;
            }else{
                this.datalist.isShowArea=0;
            }
        },
        //城市广告-写游记
        saveCityAdv:function(){
            var that=this;
            if(!that.datalist.categoryId){
                vm.$refs.prompt.toast("游记分类不能为空！");
                return;
            }
            if(!that.datalist.cityId){
                vm.$refs.prompt.toast("游记城市不能为空！");
                return;
            }
            if(!that.datalist.title){
                vm.$refs.prompt.toast("游记标题不能为空！");
                return;
            }
            if(!that.datalist.coverImgSrc){
                vm.$refs.prompt.toast("游记封面不能为空！");
                return;
            }
            var content =$('#content').getValue();
            that.datalist.content=content;
            if(!that.datalist.content){
                vm.$refs.prompt.toast("游记内容不能为空！");
                return;
            }
            console.log(that.datalist);
            var formData = new FormData();
            formData.append("categoryId", that.datalist.categoryId);
            formData.append("cityId", that.datalist.cityId);
            formData.append("coverImgWidth",that.datalist.coverImgWidth);
            formData.append("coverImgHeight", that.datalist.coverImgHeight);
            formData.append("title", that.datalist.title);
            formData.append("isShowArea", that.datalist.isShowArea);
            formData.append("content", that.datalist.content);
            formData.append("coverImgSrc",that.datalist.coverImgSrc);
            formData.append("loginUid",userInfo.id);
            var products=[];
            if(that.myGoodsList){
                var productIdList={};
                for(var i=0; i<that.myGoodsList.length; i++){
                    productIdList.productId=that.myGoodsList[i].productId;
                    // category.push($.param(categorys))
                    products.push(productIdList)
                }
                formData.append("products",products);
            }
            console.log(products)
            $.ajax({
                url:host+'/v1/cityAdv/saveCityAdv',
                type:'POST',
                data:formData,
                dataType:'json',
                async: false,
                cache: false,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                success:function(data){
                    console.log(data);
                    if(data.code==0){
                        sessionStorage.removeItem(key);
                        sessionStorage.removeItem(categoryName);
                        sessionStorage.removeItem(cityN);
                        sessionStorage.removeItem(dataList);
                        sessionStorage.removeItem(contentN);
                        location.href="/see/cityAd"
                    }
                },
                error:function(){
                    vm.$refs.prompt.toast("写游记失败！")
                }
            });
        },
        //购买展位
        buyBooth:function(){
            var that=this;
            if(that.number>=5){
                that.getIsScreen();
                vm.$refs.prompt.toast("最多可以购买5个展位");
            }else{
                this.ajax({url:"/cityAd/buyBooth",data:{loginUid:userInfo.id},dataType:"json",method:"post"},function(b){
                    if(b.json.code==0){
                        that.number++;
                        console.log(that.number)
                        that.getIsScreen();
                        vm.$refs.prompt.toast("购买成功！");
                    }else{
                        that.getIsScreen();
                        vm.$refs.prompt.toast(b.json.msg)
                    }
                });
            }
        },
        getGoods:function(item){
            sessionStorage.setItem(categoryName,JSON.stringify(this.categoryName));
            sessionStorage.setItem(cityName,JSON.stringify(this.cityName));
            sessionStorage.setItem(data,JSON.stringify(this.datalist));
            var contents=$('#content').getValue();
            sessionStorage.setItem(content,JSON.stringify(contents));
            if(item&&item!=1){
                location.href="/myGoods?type=1&uid="+userInfo.id+"&productId="+item.productId+"&number="+this.number;
            }else{
                location.href="/myGoods?type=1&uid="+userInfo.id+"&number="+this.number;
            }
        }
    }
    ,watch:{
        categoryList:function(){
            var that=this;
            that.categoryList.forEach(function(val,key){
                that.categoryList1.push({"id":val.categoryId, "value":val.name});
            });
            console.log(that.categoryList1);
        },
        datalist:{
            handler:function(val, oldVal) {
                var that=this;
                console.log(that.datalist)
                var content =$('#content').getValue();
                that.datalist.content=content;
                if(that.datalist.categoryId&&that.datalist.cityId&&that.datalist.title&&that.datalist.isShowArea&&that.datalist.coverImgSrc&&that.datalist.content!='<div class="placeholader" style="pointer-events: none;">请输入文章正文内容</div>'){
                    that.num=true;
                }
            },
            deep: true
        }
    }
});
$('#content').artEditor({
    imgTar: '#imageUpload',
    limitSize: 5,   // 兆
    showServer: true,
    uploadUrl: host+'/v1/friend/savePostPhoto',
    data: {loginUid:vm.datalist.loginUid},
    uploadField: 'image',
    breaks: false,
    placeholader: '请输入文章正文内容',
    validHtml: ["<br/>"],
    formInputId: 'target',
    uploadSuccess: function (res) {
        console.log("ddddd")
        console.log(res)
        // 这里是处理返回数据业务逻辑的地方
        // `res`为服务器返回`status==200`的`response`
        // 如果这里`return <path>`将会以`<img src='path'>`的形式插入到页面
        // 如果发现`res`不符合业务逻辑
        // 比如后台告诉你这张图片不对劲
        // 麻烦返回 `false`
        // 当然如果`showServer==false`
        // 无所谓咯
        // return res;
    },
    uploadError: function (status, error) {
        //这里做上传失败的操作
        //也就是http返回码非200的时候
        alert('网络异常' + status)
    }
});
var map = new AMap.Map('iCenter', {
    resizeEnable: true
});
showCityInfo();
//获取用户所在城市信息
function showCityInfo() {
    //实例化城市查询类
    var citysearch = new AMap.CitySearch();
    //自动获取用户IP，返回当前城市
    citysearch.getLocalCity(function(status, result) {
        console.log(result);
        if (status === 'complete' && result.info === 'OK') {
            if (result && result.city) {
                vm.cityName1=result.city;
            }
        } else {
            vm.cityName1=result.info;
        }
    });
}
function demo_report(src) {
    var img = new Image();
    img.onload=function(){
        vm.datalist.coverImgWidth = img.width;
        vm.datalist.coverImgHeight = img.height;

    };
    $(".circleSetup-cover").append(img);
    img.src = src;
}